﻿using Gameplay.RTS.ScriptConfig;
using UnityEngine;
using Yoozoo.Gameplay.RTS.Proto;

namespace Yoozoo.Gameplay.RTS
{
    public class AttackHit
    {
        private float _amount;
        private ERtsSkillState _status;
        private MarchEntity _march;
        private float _startTime;
        private MarchEntity _target;
        private float _endTime;
        private int _textType;
        private bool _isRelatedToSelf;
        private Vector3 _playPosition;

        private long _marchTid;
        private long _targetTid;
        
        internal void OnReset()
        {
            
        }

        internal void Initialize(MarchEntity march, int amount, float startTime, MarchEntity target, int textType)
        {
            _amount = amount;
            _status = ERtsSkillState.NotPlay;
            _march = march;
            _startTime = startTime;
            _target = target;
            _endTime = 1;
            _textType = textType;
            _isRelatedToSelf = target != null && target.Team == MarchHelper.TeamSelf || march != null && march.Team == MarchHelper.TeamSelf;
            _marchTid = _march?.Tid ?? 0;
            _targetTid = _target?.Tid ?? 0;
        }
        internal void Initialize(AttackHitTemp hitTemp)
        {
            _amount = hitTemp.Amount;
            _status = ERtsSkillState.NotPlay;
            _startTime = hitTemp.StartTime;
            _endTime = 1;
            _textType = hitTemp.TextType;
            _isRelatedToSelf = false;
            _playPosition = hitTemp.Position;
        }
        internal void Update(float curTime)
        {
            if (_status == ERtsSkillState.NotPlay)
            {
                if (_march != null && !_march.MarchInfo.IsInBattleState)
                {
                    _startTime = curTime + 0.5f;
                }
                _endTime = _startTime + 2;
                _status = ERtsSkillState.AttackRoundStart;
            }
            if (curTime >= _startTime)
            {
                Play();
                PlayTemp();
                _status = ERtsSkillState.Playing;
            }

            if (curTime >= _endTime && _status == ERtsSkillState.Playing)
            {
                _status = ERtsSkillState.End;
            }
        }

        private void Play()
        {
            var marchValid = _march != null &&
                             (!_march.IsReadyToRemove() || _march.MarchInfo.MarchType != MarchType.DEFENCE_6) &&
                             _march.Tid == _marchTid;
            var targetValid = _target != null &&
                             (!_target.IsReadyToRemove() || _target.MarchInfo.MarchType != MarchType.DEFENCE_6) &&
                             _target.Tid == _targetTid;
            if (!marchValid || !targetValid)
            {
                return;
            }
            
            if (_amount != 0)
            {
                FlyTextAudio audioType = FlyTextAudio.NONE;
                string amountTxt = string.Empty;
                if (_amount > 0)
                {
                    amountTxt = $"+{amountTxt}";
                }
                else
                {
                    //是自己的部队需要播放飘字音效
                    if (_target != null && _target.RtsServerInfo != null && _target.RtsServerInfo.team == (int)MarchTeam.Self)
                    {
                        audioType = FlyTextManager.Instance.GetFlyTextAudioBySoldierType(_target.RtsServerInfo.SoldierType);
                    }
                    amountTxt = _amount.ToString();
                }
                var proiority = _isRelatedToSelf ? 1 : 0;

                if (_march != null)
                {
                    string animName = default;
                    if (_textType == FlyTextManager.PassiveAttack)
                    {
                        animName = "sfx_ui_flytext_lj1"; //右边
                    }
                    if (_target != null)
                    {
                        var targetPosition = _target.Position;
                        var targetScreenPos = WorldCameraManager.mainCamera.WorldToScreenPoint(targetPosition);
                        var myScreenPos = WorldCameraManager.mainCamera.WorldToScreenPoint(_march.Position);
                        if (targetScreenPos.x > myScreenPos.x)
                            animName = "sfx_ui_flytext_lj2";
                        
                        RTSLuaCallCSharpManager.FlyText(_textType, _march.Position, amountTxt, 1, _target.Position,
                            null, proiority, audioType,animName);
                    }
                    else
                    {
                        RTSLuaCallCSharpManager.FlyText(_textType, _march.Position, amountTxt, 1, default, null,
                            proiority, audioType, animName);
                    }

                    _amount = 0;
                }
            }
        }

        private void PlayTemp()
        {
            if (_amount != 0)
            {
                FlyTextAudio audioType = FlyTextAudio.NONE;
                string amountTxt = string.Empty;
                if (_amount > 0)
                {
                    amountTxt = $"+{amountTxt}";
                }
                else
                {
                    amountTxt = _amount.ToString();
                }
                var proiority = _isRelatedToSelf ? 1 : 0;

                if (_playPosition != default)
                {
                    RTSLuaCallCSharpManager.FlyText(_textType, _playPosition, amountTxt, 1, default, null, proiority, audioType);
                    _amount = 0;
                }
            }
        }

        internal void Dispose()
        {
            _march = null;
            _status = ERtsSkillState.NotPlay;
            _target = null;
            _startTime = 0;
            _playPosition = default;
            _marchTid = 0;
            _targetTid = 0;
        }

        internal bool IsEnd()
        {
            return _status == ERtsSkillState.End;
        }

      
    }
}
